<?php
namespace Home\Biz;

class AdminBiz{
	/**
	 * 页面登陆验证
	 */
	public static function auditLogin(){
		//验证当前用户是否登陆
		$login_user_id = cookie(C('AdminConfig.login_cookie_name'));
		if(empty($login_user_id)){
			//未登录
			redirect(C('UrlConfig.login_url'));
			return;
		}
		 
		//验证当前用户是否通过审核
		$promoter_en = M('promoterInfo')->where("promoter_id='%s'", $login_user_id)->find();
		if(empty($promoter_en)){
			//不存在用户
			redirect(C('UrlConfig.audit_error_url').'?type=1&promoter_id=$login_user_id');
			return;
		}
		 
		if($promoter_en['status'] == 0){
			//用户待审核
			redirect(C('UrlConfig.audit_error_url').'?type=2&promoter_id=$login_user_id');
			return;
		}
		 
		if($promoter_en['status'] == 2){
			//用户审核拒绝
			redirect(C('UrlConfig.audit_error_url').'?type=3&promoter_id=$login_user_id');
			return;
		}
	}
	
	
	/**
	 * 用户登陆
	 */
	public static function login($cellphone, $password) {
		$promoter_en = M('promoterInfo')->where("cellphone='%s' OR promoter_id='%s'", array($cellphone, $cellphone))->find();
		
		if(empty($promoter_en)){
			return '不存在的用户';
		}
		
		$encrypt_password = md5(C('SystemConfig.encrypt_key').$promoter_en['promoter_id'].$password);
		
		if($promoter_en['password'] != $encrypt_password){
			return '用户名密码不匹配';
		}
		
		cookie(C('AdminConfig.login_cookie_name'), 
				$promoter_en['promoter_id'], 
				array('expire'=>C('AdminConfig.login_cookie_expire'),
						'domain'=>C('AdminConfig.login_cookie_domain')));
		
		
		$safe_code = md5($promoter_en['promoter_id'].C('SystemConfig.encrypt_key').date("y-m-d", time()));
		
		cookie(C('AdminConfig.login_safecode_cookie_name'),
				$safe_code,
				array('expire'=>C('AdminConfig.login_cookie_expire'),
						'domain'=>C('AdminConfig.login_cookie_domain')));
		return '';
	}
	

	/**
	 * Description: 新建推广人
	 * @version 2016年8月31日 下午1:21:45 
	 * @author lin.yujiao@whaley.cn
	 */
	private function createPromoter($employee_name, $cellphone, $user){
		$user_id = PromoterBiz::generateUserId();
			
		$promoter_en	= array();
		$promoter_id					= PromoterBiz::generatePromoterId();
		$share_code						= PromoterBiz::generateShareCode();
			
		$promoter_en['promoter_id']		= $promoter_id;
		$promoter_en['promoter_name']	= $employee_name;
		$promoter_en['user_id']		    = $user_id;
		$promoter_en['share_code']		= $share_code;
		$promoter_en['promoter_type']	= 1;
		$promoter_en['cellphone']		= $cellphone;
		$promoter_en['real_name']		= $employee_name;
		$promoter_en['status']			= 1;
		$promoter_en['create_user']		= $user;
		$promoter_en['create_date']		= date("y-m-d H:i:s", time());
		M('promoterInfo')->add($promoter_en);
			
			
		//写入user_info
		$user_info_en = array(
				'user_id'	=> $user_id,
				'user_name' => $promoter_en['real_name'],
				'real_name' => $promoter_en['real_name'],
				'cellphone' => $promoter_en['cellphone'],
				'sex' 		=> $promoter_en['sex'],
				'account_source'=> 5,
				'register_date' => date("y-m-d H:i:s", time()),
				'register_ip'	=> get_client_ip(),
				'register_source' 	=> 5,
				'status'		=> 1,
				'create_user'	=> $user,
				'create_date'	=> date("y-m-d H:i:s", time())
		);
		M('userInfo', '', 'DB_trade')->add($user_info_en);
	
		return $promoter_en;
	}
	/**
	 * Description: 员工登录cookie缓存
	 * @version 2016年8月31日 下午1:24:14 
	 * @author lin.yujiao@whaley.cn
	 */
	private function employeeLoginCookie($promoter){
		cookie(C('AdminConfig.login_cookie_name'),
				$promoter['promoter_id'],
				array('expire'=>C('AdminConfig.login_cookie_expire'),
						'domain'=>C('AdminConfig.login_cookie_domain')));
		
				$safe_code = md5($promoter['promoter_id'].C('SystemConfig.encrypt_key').date("y-m-d", time()));
		
				cookie(C('AdminConfig.login_safecode_cookie_name'),
						$safe_code,
						array('expire'=>C('AdminConfig.login_cookie_expire'),
								'domain'=>C('AdminConfig.login_cookie_domain')));
	}
	
	/**
	 * 验证用户注册信息
	 */
	public static function verifyRegister($promoter_en){
		//参数验证 start
		if(empty($promoter_en['promoter_type'])){
			return '类型选择错误';
		}
		if(empty($promoter_en['cellphone'])){
			return '手机不能为空';
		}
		if(!StringBiz::isCellphone($promoter_en['cellphone'])){
			return '手机格式不符合';
		}
		if(empty($promoter_en['sex'])){
			return '性别选择错误';
		}
		if(empty($promoter_en['real_name'])){
			return '真实姓名不能为空';
		}
		if(empty($promoter_en['password'])){
			return '密码不能为空';
		}
		if(empty($promoter_en['alipay_id'])){
			return '支付宝账户不能为空';
		}
		if(empty($promoter_en['alipay_name'])){
			return '支付宝姓名不能为空';
		}
		if(empty($promoter_en['id_card_no'])){
			return '身份证号码不能为空';
		}
		if(!StringBiz::isIdentity($promoter_en['id_card_no'])){
			return '身份证格式不正确';
		}
		
		if(empty($promoter_en['district_id'])){
			return '省市区选择错误';
		}
		if(empty($promoter_en['address'])){
			return '地址不能为空';
		}
		
		if(!StringBiz::isAddress($promoter_en['address'])){
			return '地址信息不正确';
		}
		
		//校验手机号是否存在
		$exist_promoter_en = M('promoterInfo')->where("cellphone='%s'", $promoter_en['cellphone'])->find();
		if(!empty($exist_promoter_en)){
			return '这个手机号码已经注册过了';
		}
		
		//校验推荐码
		if(!empty($recommend_share_code)){
			$recommend_en = M('promoterInfo')->where("share_code='%s'", $recommend_share_code)->find();
			if(empty($recommend_en)){
				return '不存在的推荐码';
			}
			else{
				$promoter_en['parents_promoter_id']	= $recommend_en['promoter_id'];
			}
		}
		
		//省市区
		$district_id = $promoter_en['district_id'];
		$lst_region_en = M('', '', 'DB_system')->query("
				SELECT * FROM province p
				INNER JOIN city c
				ON p.`province_id` = c.`province_id`
				INNER JOIN district d
				ON c.`city_id` = d.`city_id`
				WHERE district_id = '$district_id'
				");
		
		
		if(empty($lst_region_en)){
			return '错误的区域选择';
		}
		//参数验证 end
		return '';
	}
	
	
	/**
	 * 用户注册
	 */
	public static function register($promoter_en, $recommend_share_code){
		
		//region 1、参数验证 
		if(empty($promoter_en['promoter_type'])){
			return '类型选择错误';
		}
		if(empty($promoter_en['cellphone'])){
			return '手机不能为空';
		}
		if(!StringBiz::isCellphone($promoter_en['cellphone'])){
			return '手机格式不符合';
		}
		if(empty($promoter_en['sex'])){
			return '性别选择错误';
		}
		if(empty($promoter_en['real_name'])){
			return '真实姓名不能为空';
		}
		if(empty($promoter_en['password'])){
			return '密码不能为空';
		}
		if(empty($promoter_en['alipay_id'])){
			return '支付宝账户不能为空';
		}
		if(empty($promoter_en['alipay_name'])){
			return '支付宝姓名不能为空';
		}
		if(empty($promoter_en['id_card_no'])){
			return '身份证号码不能为空';
		}
		if(!StringBiz::isIdentity($promoter_en['id_card_no'])){
			return '身份证格式不正确';
		}
		if(empty($promoter_en['id_card_image_list'])){
			return '身份证图片不能为空';
		}
		
		
		
		if($promoter_en['promoter_type'] == 2){
			if(empty($promoter_en['business_license_image'])){
				return '营业执照号码不能为空';
			}
			if(empty($promoter_en['organization_image'])){
				return '组织机构证不能为空';
			}
			if(empty($promoter_en['tax_registration_image'])){
				return '税务登记证不能为空';
			}
		}
		
		if(empty($promoter_en['district_id'])){
			return '省市区选择错误';
		}
		if(empty($promoter_en['address'])){
			return '地址不能为空';
		}
		
		if(!StringBiz::isAddress($promoter_en['address'])){
			return '地址信息不正确';
		}
		
		//校验手机号是否存在
		$exist_promoter_en = M('promoterInfo')->where("cellphone='%s'", $promoter_en['cellphone'])->find();
		if(!empty($exist_promoter_en)){
			return '这个手机号码已经注册过了';
		}
		
		//校验推荐码
		if(!empty($recommend_share_code)){
			$recommend_en = M('promoterInfo')->where("share_code='%s'", $recommend_share_code)->find();
			if(empty($recommend_en)){
				return '不存在的推荐码';
			}
			else{
				$promoter_en['parents_promoter_id']	= $recommend_en['promoter_id'];
				$promoter_en['has_superior'] = 1;
			}
		}
		
		//省市区
		$district_id = $promoter_en['district_id'];
		$lst_region_en = M('', '', 'DB_system')->query("
				SELECT * FROM province p
				INNER JOIN city c
				ON p.`province_id` = c.`province_id`
				INNER JOIN district d
				ON c.`city_id` = d.`city_id`
				WHERE district_id = '%s'
				", $district_id);
		
		
		if(empty($lst_region_en)){
			return '错误的区域选择';
		}
		else{
			$region_en = $lst_region_en[0];
		}
		//endregion 1、参数验证
		

		//region 2、生成promoter_info数据并写入
		$promoter_id= PromoterBiz::generatePromoterId();
		$share_code	= PromoterBiz::generateShareCode();
		$promoter_en['promoter_id']	= $promoter_id;
		$promoter_en['share_code']	= $share_code;
		
		//密码
		$encrypt_password = md5(C('SystemConfig.encrypt_key').$promoter_id.$promoter_en['password']);
		$promoter_en['password'] = $encrypt_password;
		
		$promoter_en['province_id']		= $region_en['province_id'];
		$promoter_en['province_name']	= $region_en['province_name'];
		$promoter_en['city_id']			= $region_en['city_id'];
		$promoter_en['city_name']		= $region_en['city_name'];
		$promoter_en['district_id']		= $region_en['district_id'];
		$promoter_en['district_name']	= $region_en['district_name'];
		
		$user_id = PromoterBiz::generateUserId();
		$promoter_en['user_id']	= $user_id;
		M('promoterInfo')->add($promoter_en);
		//endregion 2、生成promoter_info数据并写入


		//region 3、存在推荐人则新写入推荐人关系表，并更新推荐人的has_subordinate字段
		if(!empty($promoter_en['parents_promoter_id'])){

			//只有推荐人不存在上级才可写入关系
			if($recommend_en['has_superior'] == 0){
				//region a、写入promoter_relation表
				$promoter_relation = array(
					'promoter_id'	=> $recommend_en['promoter_id'],
					'promoter_name'	=> $recommend_en['promoter_name'],
					'sub_promoter_id'	=> $promoter_en['promoter_id'],
					'sub_promoter_name'	=> $promoter_en['promoter_name'],
					'sub_promoter_level'=> 2,
					'sub_revenue_rate'	=> 0.9,
					'status'		=> 1,
					'create_user'	=> $promoter_en['create_user'],
					'create_date'	=> date("y-m-d H:i:s", time())
				);
					
				M('promoter_relation')->add($promoter_relation);
				//endregion a、写入promoter_relation表


				//region b、更新推荐人的has_subordinate、sub_revenue_rate字段
				$update_promoterEn = array(
					'has_subordinate'	=> 1,
					'sub_revenue_rate'	=> 0.9
				);
				M('promoter_info')->where("promoter_id='%s'", $recommend_en['promoter_id'])->setField($update_promoterEn);
				//region b、更新推荐人的has_subordinate、sub_revenue_rate字段
			}
		}
		//endregion 3、存在推荐人则新写入推荐人关系表，并更新推荐人的has_subordinate字段

		//region 4、写入user_info
		$user_info_en = array(
				'user_id'	=> $user_id,
				'user_name' => $promoter_en['real_name'],
				'real_name' => $promoter_en['real_name'],
				'cellphone' => $promoter_en['cellphone'],
				'sex' 		=> $promoter_en['sex'],
				'account_source'=> 5,
				'province_id'	=> $promoter_en['province_id'],
				'province_name' => $promoter_en['province_name'],
				'city_id'		=> $promoter_en['city_id'],
				'city_name' 	=> $promoter_en['city_name'],
				'district_id'	=> $promoter_en['district_id'],
				'district_name' => $promoter_en['district_name'],
				'address'		=> $promoter_en['address'],
				'register_date' => date("y-m-d H:i:s", time()),
				'register_ip'	=> get_client_ip(),
				'register_source' 	=> 5,
				'status'		=> 0,
				'create_user'	=> $promoter_en['create_user'],
    			'create_date'	=> date("y-m-d H:i:s", time())
		);
		M('userInfo', '', 'DB_trade')->add($user_info_en);
		//endregion 4、写入user_info
		
		cookie(C('AdminConfig.login_cookie_name'),
				$promoter_en['promoter_id'],
				array('expire'=>C('AdminConfig.login_cookie_expire'),
						'domain'=>C('AdminConfig.login_cookie_domain')));
	}
	
	
}